﻿using Datacollection.Services.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Datacollection
{
    public partial class SPsettings : Form
    {
        // 【关键】暴露公共属性，供主窗口读取
        public string PortName { get; set; }
        public int BaudRate { get; set; }
        public Parity Parity { get; set; }
        public int DataBits { get; set; }
        public StopBits StopBits { get; set; }

        public SPsettings()
        {
            InitializeComponent();
        }

        private void SPsettings_Load(object sender, EventArgs e)
        {
            this.comboBoxPort.SelectedIndex = 0;
            this.comboBoxBaudRate.SelectedIndex = 5;
            this.comboBoxDataBits.SelectedIndex = 3;
            this.comboBoxParity.SelectedIndex = 0;
            this.comboBoxStopBits.SelectedIndex = 0;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            var logger = LogServiceLocator.Logger;
            try
            {
                // 校验必填项（串口必须选择）
                if (comboBoxBaudRate.SelectedItem == null)
                {
                    MessageBox.Show("请选择串口号！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // 转换并赋值给公共属性
                PortName = comboBoxPort.SelectedItem.ToString();
                BaudRate = Convert.ToInt32(comboBoxBaudRate.SelectedItem);
                DataBits = Convert.ToInt32(comboBoxDataBits.SelectedItem);
                //Parity = (Parity)Enum.Parse(typeof(Parity), comboBoxParity.SelectedItem.ToString());
                //StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBoxStopBits.SelectedItem.ToString());
                switch (comboBoxParity.SelectedIndex)
                {
                    case 0:
                        Parity = Parity.None;
                        break;
                    case 1:
                        Parity = Parity.Odd;
                        break;
                    case 2:
                        Parity = Parity.Even;
                        break;
                }
                switch (comboBoxStopBits.SelectedIndex)
                {
                    case 0:
                        StopBits = StopBits.One;
                        break;
                    case 1:
                        StopBits = StopBits.OnePointFive;
                        break;
                    case 2:
                        StopBits = StopBits.Two;
                        break;
                }

                // 设置对话框结果为OK，主窗口可通过此判断是否保存
                DialogResult = DialogResult.OK;
                logger.Info("保存串口设置成功！");
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"参数转换失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 取消按钮：直接关闭
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
